﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.ModelBinding;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using PharmacyBussinesEntities;
using PharmacyService;

namespace PharmacyWeb.Almacen
{
    public partial class frmLaboratorio : System.Web.UI.Page
    {
        [Dependency]
        public ILaboratorioService LaboratorioService { get; set; }
        

        protected void Page_Load(object sender, EventArgs e)
        {
            var accion = Request.QueryString["accion"];
            var idLaboratorio = Request.QueryString["id"];

            if (accion != null && accion == "nuevo")
            {
                fvLaboratorio.DefaultMode = FormViewMode.Insert;
            }
            else if (accion != null && accion == "eliminar" && idLaboratorio != null)
            {
                var id = Int32.Parse(idLaboratorio.ToString());

                LaboratorioService.RemoveLaboratorio(id);
                Response.Redirect("frmListLaboratorio.aspx");
            }
        }

        public Laboratorio GetLaboratorio([QueryString("identificador")] Int32? id)
        {
            Laboratorio laboratorio = null;
            if (id.HasValue)
                laboratorio = LaboratorioService.GetFromLaboratorioById(id.Value);

            return laboratorio;
        }

        public void UpdateLaboratorio(Laboratorio laboratorio)
        {
            if (ModelState.IsValid)
            {
                LaboratorioService.UpdateLaboratorio(laboratorio);
                Response.Redirect("frmListLaboratorio.aspx");
            }
        }

        public void InsertLaboratorio(Laboratorio laboratorio)
        {
            laboratorio.Id = 0;
            if (ModelState.IsValid)
            {
                LaboratorioService.AddLaboratorio(laboratorio);
                Response.Redirect("frmListLaboratorio.aspx");
            }
        }
    }
}